package com.ego.product.web;

import com.ego.common.constant.SystemConstant;
import com.ego.common.to.member.MemberTo;
import com.ego.common.util.Result;
import com.ego.common.vo.coupon.SeckillParam;
import com.ego.product.feign.CouponFeign;
import com.ego.product.interceptor.LoginInterceptor;
import com.ego.product.vo.coupon.SeckillVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.concurrent.ExecutionException;

/**
 * @program: ego
 * @description: 活动
 * @author: KamTang
 * @create: 2023-01-05 09:26
 **/
@Controller
@RequestMapping("/coupon")
public class CouponController {

    @Autowired
    private CouponFeign couponFeign;

    @PostMapping("/kill")
    @ResponseBody
    public Result kill(@Valid @RequestBody SeckillParam param) throws ExecutionException, InterruptedException {
        // 判断是否登录
        MemberTo member = LoginInterceptor.threadLocal.get();
        SeckillVo seckillVo = new SeckillVo();
        seckillVo.setKillId(param.getKillId());
        seckillVo.setCode(param.getCode());
        seckillVo.setNum(param.getNum());
        seckillVo.setMemberId(member.getId());
        Result killResp = couponFeign.kill(seckillVo);
        // 抢购成功
        if (killResp.getCode() == SystemConstant.Status.SUCCESS.getCode()) {
            return Result.ok();
        }
        return Result.error();
    }
}
